package com.atguigu.service.impl;

import com.atguigu.dao.BookDao;
import com.atguigu.dao.impl.BookDaoImpl;
import com.atguigu.pojo.Book;
import com.atguigu.pojo.PageInfo;
import com.atguigu.service.BookService;

import java.util.List;

/**
 * @Author luoyin
 * @Date 17:14 2022/7/18
 * @Description 描述
 **/
public class BookServiceImpl implements BookService {
    @Override
    public List<Book> selectAll() throws Exception {
        BookDao bookDao = new BookDaoImpl();
        return bookDao.selectAll();
    }

    @Override
    public PageInfo<Book> selectByPage(int currentPage) throws Exception {
        PageInfo<Book> pageInfo = new PageInfo<>();
        //设置当前页数
        pageInfo.setCurrentPage(currentPage);
        //每页记录数
        int pageSize = 5;
        pageInfo.setPageSize(pageSize);
        BookDao bookDao = new BookDaoImpl();
        //总记录数
        Long totalSize = bookDao.selectTotalSize();
        pageInfo.setTotalSize(totalSize);
        //总页数
        Long totalPage = totalSize % pageSize == 0 ? totalSize/pageSize : (totalSize/pageSize + 1);
        pageInfo.setTotalPage(totalPage);
        //图书数据
        int begin = (currentPage - 1) * pageSize;
        List<Book> data = bookDao.selectLimit(begin,pageSize);
        pageInfo.setData(data);
        return pageInfo;
    }

    @Override
    public int add(Book book) throws Exception {
        BookDao bookDao = new BookDaoImpl();
        book.setImgPath("static/uploads/wangjiaer.jpg");
        return bookDao.add(book);
    }

    @Override
    public int update(Book book) throws Exception {
        BookDao bookDao = new BookDaoImpl();
        return bookDao.update(book);
    }

    @Override
    public Book selectById(int bookId) throws Exception {
        BookDao bookDao = new BookDaoImpl();
        return bookDao.selectById(bookId);
    }

    @Override
    public int delete(int bookId) throws Exception {
        BookDao bookDao = new BookDaoImpl();
        return bookDao.delete(bookId);
    }

    @Override
    public PageInfo<Book> selectList(int currentPage, Double floorPrice, Double peakPrice) throws Exception {
        PageInfo<Book> pageInfo = new PageInfo<>();
        //设置当前页数
        pageInfo.setCurrentPage(currentPage);
        //设置每页记录数
        int pageSize = 5;
        pageInfo.setPageSize(pageSize);
        //设置总记录数
        BookDao bookDao = new BookDaoImpl();
        Long totalSize = bookDao.selectListTotalSize(floorPrice,peakPrice);
        pageInfo.setTotalSize(totalSize);
        //设置总页数
        Long totalPage = totalSize % pageSize == 0 ? (totalSize / pageSize) : (totalSize / pageSize +1);
        pageInfo.setTotalPage(totalPage);
        //设置数据
        List<Book> data = bookDao.selectList(floorPrice,peakPrice,(currentPage - 1)*pageSize,pageSize);
        pageInfo.setData(data);
        return pageInfo;
    }


}
